package com.cy.pj.sys.web.controller;

import com.cy.pj.common.util.PageUtil;
import com.cy.pj.sys.pojo.SysLog;
import com.cy.pj.sys.service.SysLogService;
import com.cy.pj.sys.web.pojo.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/log/")
public class SysLogController {
    @Autowired
    private SysLogService service;

    @GetMapping("{id}")
        public JsonResult doFindById (@PathVariable  Long id ){
            return new JsonResult(service.findbyId(id));
        }
    @DeleteMapping("{ids}")
    public JsonResult doDeleteById (@PathVariable Long...ids) {
        service.deleteLog(ids);
        return new JsonResult("delete ok");
    }

    /**
     * 基于条件分页查询日志信息
     * @param sysLog
     * @return
     * url：http://localhost/log/
     * http://localhost/log/?username=jason
     * http://localhost/log/?username=jason&status=1
     * x
     */
    @GetMapping
    public JsonResult  doFindLogs(SysLog sysLog){
//        PageInfo<SysLog>pageInfo=PageUtil.startPage()
//                .doSelectPageInfo(()->service.findLogs(sysLog));
//        return  new JsonResult(pageInfo);
//        pageHelper是存在数据持久层的 mybatis的插件
        return new JsonResult(PageUtil.startPage()//启动分页查询拦截
                .doSelectPageInfo(()->//分页查询日志信息
//                        调用业务层方法执行查询，查询结果底层会存储到pageInfo对象
                    service.findLogs(sysLog)
                ));
        //假如应用了aop设计，sysLogService变量指向的是代理对象
    }
    @PostMapping
    public JsonResult doSaveLogs(@RequestBody SysLog log){
        service.saveLog(log);
        return  new JsonResult("insert OK");
    }
}

